function mindist = mindistGrpOfferGS_out_myopic2(theta,match)

NN = match.NN;
JJ = match.JJ;
J = match.J;
N = match.N/J;
M = match.M;
S = match.S;

% Parcel preferences
gamma = [1 theta(1,1:NN - 1)];
% Firm preferences
delta = [1 theta(1,NN:NN + JJ - 2)];
alpha = theta(1,NN + JJ - 1);

NN = size(gamma,2); JJ = size(delta,2);

shareHOLD = match.share0;      % J*N x 1
% MarketCount0 = match.MarketCount0;

% Firm Value of parcels
ParcelUtility = match.ParcelIndex*gamma';
% Parcel value of firms (and leases)
FirmUtility = match.FirmMatrix*delta';

%% Market Match

SampleMMean_SimMoment       = zeros(M,JJ + NN,S);
SampleMVar_SimMoment        = zeros(M,JJ + NN,S);
SampleMCov_SimMoment        = zeros(M,JJ*NN,S);

SampleJMean_SimMoment       = zeros(J,NN,S);
SampleJCov_SimMoment        = zeros(J,NN*JJ,S);
SampleJVar_SimMoment        = zeros(J,NN,S);
SampleJDist_SimMoment       = zeros(J,NN*JJ,S);

SampleJntDist_SimMoment     = zeros(NN*JJ,S);
SampleCov_SimMoment         = zeros(NN*JJ,S);

Share_Sim                   = zeros(M*J,S);
ShareMean_Sim               = zeros(M,NN + JJ,S);
ShareFirm_Sim               = zeros(M,S);
ShareCov_Sim                = zeros(M,NN + JJ,S);

parfor s = 1:S

    share0 = shareHOLD; 
    
    % Sub-market profits and index
    Offer_sub  = ParcelUtility + alpha*share0 + match.ParcelError_Sim(:,s); 
    Accept_sub = FirmUtility + match.FirmError_Sim(:,s); 

    Aoffer  = reshape(Offer_sub,[J,N]); 
    Baccept = reshape(Accept_sub,[J,N]);

    % Sort preferences
    rankA = SortingMatInC_out(Aoffer); 
    rankB = SortingResMatInC_out(Baccept);
            
    % GS algorithm
    [~,FirmMatchOUT] = MatchAofferBaccept_GS_out(rankA,rankB,match.FirmMatchConstraint);
    shareOUT = ShareMarket(FirmMatchOUT',match.MarketCount0,J);

    % Firm group royalty/clause means for moments    
    [~,~,GpPMeanShare,GpFMeanShare,GpShareMarket,GpShareFirm,GpFMean,GpParcelCov,GpFirmCov] = GroupMoments_share2(match.ParcelIndex,match.FirmMatrix,FirmMatchOUT,J,match.MarketIndex0,match.MarketCount0,shareOUT);
    [~,~,PMean,FMean,JntDist,Cov,Pvar,Fvar,PFcovar,Pmean_firm,Pvar_firm,PFJntDist_firm,PFcovar_firm] = GroupMomentsSmall_new(match.ParcelIndex,match.FirmMatrix,FirmMatchOUT,J,match.MarketIndex0,match.MarketCount0,match.MomentSwitch);
     
    % Calculate the moments
    SampleMMean_SimMoment(:,:,s)        = [FMean PMean];
    SampleMCov_SimMoment(:,:,s)         = PFcovar;
    SampleMVar_SimMoment(:,:,s)         = [Fvar Pvar];

    SampleJMean_SimMoment(:,:,s)        = Pmean_firm;
    SampleJCov_SimMoment(:,:,s)         = PFcovar_firm;
    SampleJVar_SimMoment(:,:,s)         = Pvar_firm;
    SampleJDist_SimMoment(:,:,s)        = PFJntDist_firm;
        
    SampleJntDist_SimMoment(:,s)        = JntDist;
    SampleCov_SimMoment(:,s)            = Cov;
    
    % Calculate share - Market
    Share_Sim(:,s)          = shareOUT;
    ShareMean_Sim(:,:,s)    = [GpFMeanShare GpPMeanShare];
    ShareFirm_Sim(:,s)      = GpShareFirm;
    ShareCov_Sim(:,:,s)     = [GpFirmCov GpParcelCov];
end

clear rankA rankB Aoffer Baccept shareOUT shareStOUT
clear FirmUtility_sub ParcelUtility_sub

clear row m index s temp Group

JntDist_SimMoment    = sum(SampleJntDist_SimMoment,2)*(1/S);
Cov_SimMoment        = sum(SampleCov_SimMoment,2)*(1/S);

% Market Moments
MMean_SimMoment      = sum(SampleMMean_SimMoment,3)*(1/S);
MCov_SimMoment       = sum(SampleMCov_SimMoment,3)*(1/S);
MVar_SimMoment       = sum(SampleMVar_SimMoment,3)*(1/S);

% Firm Moments
JMean_SimMoment      = sum(SampleJMean_SimMoment,3)*(1/S);
JCov_SimMoment       = sum(SampleJCov_SimMoment,3)*(1/S);
JVar_SimMoment       = sum(SampleJVar_SimMoment,3)*(1/S);
JDist_SimMoment      = sum(SampleJDist_SimMoment,3)*(1/S);


% Market Structure Moments
Share_Sim           = sum(Share_Sim,2)*(1/S);
ShareMean_Sim       = sum(ShareMean_Sim,3)*(1/S);
ShareFirm_Sim       = sum(ShareFirm_Sim,2)*(1/S);
ShareCov_Sim        = sum(ShareCov_Sim,3)*(1/S);

%%

M_sim = [JntDist_SimMoment; Cov_SimMoment; ...
    MMean_SimMoment(:); MCov_SimMoment(:); MVar_SimMoment(:); ...
    JMean_SimMoment(:); JCov_SimMoment(:); JVar_SimMoment(:); JDist_SimMoment(:)];
M = [match.JntDist0; match.Cov0; ...
    match.MMean_Moment; match.MCov_Moment; match.MVar_Moment; ...
    match.JMean_Moment; match.JCov_Moment; match.JVar_Moment; match.JDist_Moment];

% Full share set
M_share_sim = [Share_Sim; ShareMean_Sim(:); ShareFirm_Sim; ShareCov_Sim(:)];
M_share = [match.FirmShare0; match.GpMeanShare0; match.GpShareFirm0; match.GpCovShare0];

mindist = norm([M; M_share] - [M_sim; M_share_sim]);
